home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr37 / satsfaxt.zip / CCPUTILS.ZIP / CCAPPEND.C < prev    next >
C/C++ Source or Header  |  1990-01-30  |  19KB  |  435 lines

  1. /*---------------------------------------------------------------------------*
  2.  *                   CCAPPEND.C                     *
  3.  *---------------------------------------------------------------------------*
  4.  *      This program allows the user to append the contents of the           *
  5.  *      Connection CoProcessor log queue to an ASCII file, in either a       * 
  6.  *      formatted, tab-delimited, or comma-delimited form.  Optionally,      *
  7.  *      the user may delete the contents of the log queue after appending,   *
  8.  *      thus allowing the board to operate more efficently.                  *
  9.  *                                                                           *
  10.  *      The following source code is intended to assist developers in        *
  11.  *      creating applications whic support the DCA/Intel Communicating       *
  12.  *      Applications Specification Version 1.0A.  It is provided free of     *
  13.  *      charge and on an as-is basis.  THE IMPLIED WARRANTIES OF             *
  14.  *      MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE             *
  15.  *      SPECIFICALLY EXCLUDED.  This source code may be modified, enhanced,  *
  16.  *      copied and distributed with applications that support CAS on a       *
  17.  *      royalty-free basis.                                                  *
  18.  *---------------------------------------------------------------------------*
  19.  *  History:    - Intitial release 9/14/89.                     *
  20.  *---------------------------------------------------------------------------*/
  21.  
  22. #include <stdio.h>
  23. #include <dos.h>
  24. #include <io.h>
  25. #include <malloc.h>
  26. #include <string.h>
  27. #include "cas.h"            /* Intel CAS header file.             */
  28. #include "parse.h"            /* Command line parser header file.      */
  29. #include "util.h"            /* CAS utilities header file.         */
  30.  
  31. #define SPACE_DELIMIT    1        /* Delimit output with spaces.         */
  32. #define TAB_DELIMIT    2        /* Delimit output with tabs.         */
  33. #define COMMA_DELIMIT    3        /* Delimit output with commas.         */
  34.  
  35. int DelLog = FALSE;            /* Delete log after appending.         */
  36. int Tab = FALSE;            /* Tab delimited output.             */
  37. int Comma = FALSE;            /* Comma delimited output.             */
  38. int Help = FALSE;            /* Show help text.                 */
  39. int RedirOut = FALSE;            /* Redirect output to file.          */
  40.  
  41. ECF *TCFbuffer;             /* Task Control File buffer.         */
  42. FTR *FTRbuffer;             /* File Transfer Record buffer.         */
  43. FILE *ccout;                /* Redirected stdout file.             */
  44.  
  45. STABLE SwitchTable [] =         /* Parser switch table.             */
  46. {
  47.     { 'C', BOOL, &DelLog   },
  48.     { 'c', BOOL, &DelLog   },
  49.     { 'T', BOOL, &Tab      },
  50.     { 't', BOOL, &Tab      },
  51.     { ',', BOOL, &Comma    },
  52.     { 'R', BOOL, &RedirOut },
  53.     { 'r', BOOL, &RedirOut },
  54.     { '?', BOOL, &Help       }
  55. };
  56. #define TABLESIZE (sizeof(SwitchTable)/sizeof(STABLE))
  57.  
  58. char *DescTable[] =            /* Help function description table. */
  59. {
  60.     "CCAPPEND (V1.0) Appends the contents of the log to the specified \"filename\",\n",
  61.     "in ASCII format. If \"filename\" doesn't exist, it is created. \"filename\"\n",
  62.     "may include a path (for example, c:\\sales\\record.txt).\n\n",
  63.     "The headers from the log file are added to the top of \"filename\".\n\n",
  64.     "Syntax: CCAPPEND filename [option...]\n\n"
  65. };
  66. #define DESCSIZE (sizeof(DescTable)/sizeof(DescTable[0]))
  67.  
  68. char *HelpTable[] =            /* Help function text file. */
  69. {
  70.      "  /C           Clear log after appending to log file.\n\n",
  71.      "  /T           Tab delimited log file.\n",
  72.      "  /,           Comma delimited log file.\n\n",
  73.      "  /R           Redirect output to file OUTPUT.CC.\n",
  74.      "  /?           Display help information.\n"
  75. };
  76. #define HELPSIZE (sizeof(HelpTable)/sizeof(HelpTable[0]))
  77.  
  78. /*---------------------------------------------------------------------------*
  79.  *                  GetTaskBuffer()                 *
  80.  *---------------------------------------------------------------------------*
  81.  * Gets information about the event specified by Handle.  The task control   *
  82.  * file is opened and read.  The FileHandle returned is a normal MS-DOS      *
  83.  * file handle.                                  *
  84.  *---------------------------------------------------------------------------*
  85.  * Parameters:    int Handle - The event handle of the task.             *
  86.  *        BYTE Queue - The queue (Receive, Log or Task).             *
  87.  *        char *TCFbuffer - Uninitialized ECS structure.             *
  88.  *        int *FileHandle - Filled with DOS file handle.             *
  89.  * Return:    none                                 *
  90.  *---------------------------------------------------------------------------*/
  91. GetTaskBuffer(int Handle, BYTE Queue, char *TCFbuffer, int *FileHandle)
  92. {
  93.     /* Let CAS open the Task Control File. Read the contents into TCFbuffer. */
  94.     if((*FileHandle = CASOpenFile(Handle,0,Queue)) > 0) {
  95.         read(*FileHandle, TCFbuffer, sizeof(ECF));
  96.     }
  97.     else
  98.     CASError(CASNOTCF, TRUE, -(*FileHandle));
  99. }
  100.  
  101. /*---------------------------------------------------------------------------*
  102.  *               GetFileTransferBuffer()                 *
  103.  *---------------------------------------------------------------------------*
  104.  *  Gets information about the file transfer record.  Offset specifies the   *
  105.  *  location within the task control file of the FTR record.  The offset of  *
  106.  *  the first FTR is stored at offset 10 of the TCF.  Each subsequent FTR    *
  107.  *  is 128 bytes after the first.                                            *
  108.  *---------------------------------------------------------------------------*
  109.  *  Parameters: long Offset - File Offset to begin reading.             *
  110.  *        char *FTRbuffer - Uninitialized FTR structure, filled with   *
  111.  *            file transfer record on return.                 *
  112.  *        int *FileHandle - DOS file handle of Task Control file.      *
  113.  *            (Returned from call to GetTaskBuffer()).             *
  114.  *  Return:    none                                 *
  115.  *---------------------------------------------------------------------------*/
  116. GetFileTransferBuffer(long Offset, char *FTRbuffer, int *FileHandle)
  117. {
  118.     /* Read the File Transfer Record specified by Offset. */
  119.     lseek(*FileHandle, Offset, SEEK_SET);
  120.     read(*FileHandle, FTRbuffer, sizeof(FTR));
  121. }
  122.  
  123. /*---------------------------------------------------------------------------*
  124.  *                   CorrectDate()                 *
  125.  *---------------------------------------------------------------------------*
  126.  * Converts a date as stored in a control file to a string that can be         *
  127.  * displayed on stdout.  The TCFbuffer must first be read from the control   *
  128.  * file.                                     *
  129.  *---------------------------------------------------------------------------*
  130.  * Parameters: ECF *TCFbuffer - Filled in ECS structure.             *
  131.  *           char string[] - Array to place ascii date string.         *
  132.  * Return:     none                                 *
  133.  *---------------------------------------------------------------------------*/
  134. CorrectDate(ECF *TCFbuffer, CAS_DATE *Date, CAS_TIME *Time)
  135. {
  136.     Date->Day = TCFbuffer->EventDate & 0x001F;
  137.     Date->Month = (TCFbuffer->EventDate & 0x01E0) >> 5;
  138.     Date->Year = ((TCFbuffer->EventDate & 0xFE00) >> 9) + 1980;
  139.     Time->Minute = (TCFbuffer->EventTime & 0x07E0) >> 5;
  140.     Time->Hour = (TCFbuffer->EventTime & 0xF800) >> 11;
  141. }
  142.  
  143. /*---------------------------------------------------------------------------*
  144.  *                     SpaceIt()                     *
  145.  *---------------------------------------------------------------------------*
  146.  * Default output mode. Build the file using spaces as delimiter.         *
  147.  *---------------------------------------------------------------------------*
  148.  * Parameters:    FILE *OutFile - Handle of a previously opened file for         *
  149.  *            CCAPPEND output.                         *
  150.  *        ECF *TCFbuffer - Structure storing task control info. It must*
  151.  *            have been filled with task info prior to call.         *
  152.  *        CAS_DATE *Date - Pointer to a date structure. Filled in with *
  153.  *            call to CorrectDate.                     *
  154.  *        CAS_TIME *Time - Pointer to a time structure. Filled in with *
  155.  *            call to CorrectDate.                     *
  156.  *        char action - Array filled in with action string from TCF.   *
  157.  *        char mode - Array filled in with mode string from TCF.         *
  158.  *        char status - Array filled in with status string from TCF.   *
  159.  * Return:    none                                 *
  160.  *---------------------------------------------------------------------------*/
  161. SpaceIt(FILE *OutFile, ECF *TCFbuffer, CAS_DATE *Date, CAS_TIME *Time,
  162.                 char action[], char mode[], char status[])
  163. {
  164.     fprintf(OutFile, "%02d/%02d/%2d %-2d:%02d %-8s %-6s", Date->Month,
  165.         Date->Day, Date->Year, Time->Hour, Time->Minute, action, mode);
  166.  
  167.     if(TCFbuffer->TransferType < 2)
  168.     if(TCFbuffer->EventStatus == 0)
  169.         fprintf(OutFile, "%7ld", TCFbuffer->TotalPages);
  170.     else
  171.         fprintf(OutFile, "      0");
  172.     else
  173.     if(TCFbuffer->EventStatus == 0)
  174.         fprintf(OutFile, "%7d", TCFbuffer->FilesSent);
  175.     else
  176.         fprintf(OutFile, "      0");
  177.  
  178.     fprintf(OutFile, "  %-8s", status);
  179.     fprintf(OutFile," %02d:%02d:%02d", (int)TCFbuffer->ConnectHours,
  180.                        (int)TCFbuffer->ConnectMinutes,
  181.                        (int)TCFbuffer->ConnectSeconds);
  182.     if((TCFbuffer->EventType == SEND) || (TCFbuffer->EventType == POLLED_SEND))
  183.     fprintf(OutFile,"    %-s\n",TCFbuffer->DestinationName);
  184.     else
  185.     fprintf(OutFile,"    %-s\n",TCFbuffer->SenderName);
  186. }
  187.  
  188. /*---------------------------------------------------------------------------*
  189.  *                     TabIt()                     *
  190.  *---------------------------------------------------------------------------*
  191.  * This one builds its output with tabs!                     *
  192.  *---------------------------------------------------------------------------*
  193.  * Parameters & Return : See SpaceIt.                         *
  194.  *---------------------------------------------------------------------------*/
  195. TabIt(FILE *OutFile, ECF *TCFbuffer, CAS_DATE *Date, CAS_TIME *Time,
  196.               char action[], char mode[], char status[])
  197. {
  198.     fprintf(OutFile, "%02d/%02d/%02d\t%-2d:%02d\t%s\t%s", Date->Month,
  199.         Date->Day, Date->Year, Time->Hour, Time->Minute, action, mode);
  200.  
  201.     if(TCFbuffer->TransferType < 2)
  202.     if(TCFbuffer->EventStatus == 0)
  203.         fprintf(OutFile, "\t%ld", TCFbuffer->TotalPages);
  204.     else
  205.         fprintf(OutFile, "\t0");
  206.     else
  207.     if(TCFbuffer->EventStatus == 0)
  208.         fprintf(OutFile, "\t%d", TCFbuffer->FilesSent);
  209.     else
  210.         fprintf(OutFile, "\t0");
  211.  
  212.     fprintf(OutFile, "\t%s",status);
  213.     fprintf(OutFile, "\t%02d:%02d:%02d", (int)TCFbuffer->ConnectHours,
  214.                      (int)TCFbuffer->ConnectMinutes,
  215.                      (int)TCFbuffer->ConnectSeconds);
  216.     if((TCFbuffer->EventType == SEND) || (TCFbuffer->EventType == POLLED_SEND))
  217.     fprintf(OutFile, "\t%s\n", TCFbuffer->DestinationName);
  218.     else
  219.     fprintf(OutFile, "\t%s\n", TCFbuffer->SenderName);
  220. }
  221.  
  222. /*---------------------------------------------------------------------------*
  223.  *                     CommaIt()                     *
  224.  *---------------------------------------------------------------------------*
  225.  * Build the file with commas as the delimiter.                  *
  226.  *---------------------------------------------------------------------------*
  227.  * Parameters & Return: See SpaceIt.                         *
  228.  *---------------------------------------------------------------------------*/
  229. CommaIt(FILE *OutFile, ECF *TCFbuffer, CAS_DATE *Date, CAS_TIME *Time,
  230.                 char action[], char mode[], char status[])
  231. {
  232.     fprintf(OutFile, "%02d/%02d/%2d,%-2d:%02d,%s,%s", Date->Month,
  233.         Date->Day, Date->Year, Time->Hour, Time->Minute, action, mode);
  234.  
  235.     if(TCFbuffer->TransferType < 2)
  236.     if(TCFbuffer->EventStatus == 0)
  237.         fprintf(OutFile, ",%ld", TCFbuffer->TotalPages);
  238.     else
  239.         fprintf(OutFile, ",0");
  240.     else
  241.     if(TCFbuffer->EventStatus == 0)
  242.         fprintf(OutFile, ",%d", TCFbuffer->FilesSent);
  243.     else
  244.         fprintf(OutFile, ",0");
  245.  
  246.     fprintf(OutFile, ",%s",status);
  247.     fprintf(OutFile, ",%02d:%02d:%02d", (int)TCFbuffer->ConnectHours,
  248.                     (int)TCFbuffer->ConnectMinutes,
  249.                     (int)TCFbuffer->ConnectSeconds);
  250.     if((TCFbuffer->EventType == SEND) || (TCFbuffer->EventType == POLLED_SEND))
  251.     fprintf(OutFile, ",%s\n", TCFbuffer->DestinationName);
  252.     else
  253.     fprintf(OutFile, ",%s\n", TCFbuffer->SenderName);
  254. }
  255.  
  256. /*---------------------------------------------------------------------------*
  257.  *                  ClearLog()                     *
  258.  *---------------------------------------------------------------------------*
  259.  *  Opens each control file in the log queue and writes the contents to      *
  260.  *  FileName, in either a formatted output (for viewing and printing), tab-  *
  261.  *  delimited, or comma-delimited output (for database importing.)           *
  262.  *---------------------------------------------------------------------------* 
  263.  *  Parameters: char *FileName - Name and path of output file. If the file   *
  264.  *            exists, no header is printed. If the file does not exist,*
  265.  *            a header is first printed then the log information is    *
  266.  *            printed.                             *
  267.  *        ECF *TCFbuffer - Uninitialized ECF buffer filled with log    *
  268.  *            information on return.                     *
  269.  *        FTR *FTRbuffer - Uninitialized FTR buffer filled with file   *
  270.  *            transfer information on return.                 *
  271.  *        int Delimiter - Type of delimiter to output in file.         *
  272.  *  Return:    none                                 *
  273.  *---------------------------------------------------------------------------*/
  274. ClearLog(char *FileName, ECF *TCFbuffer, FTR *FTRbuffer, int Delimiter)
  275. {
  276.     int EventHandle;            /* Event handle.                 */
  277.     int FileHandle;            /* File handles.                 */
  278.     FILE *OutFile;            /* Log output file.              */
  279.     int ErrorCode;            /* Return from CAS calls.             */
  280.     int Header = FALSE;         /* Write header to file.             */
  281.     char action[8];            /* Send, recv, etc.              */
  282.     char mode[6];            /* Fax or file.                 */
  283.     char status[9];            /* Completed or aborted.             */
  284.     char buffer[81];            /* Buffer for log fields.             */
  285.     CAS_DATE *Date;            /* CAS date structure.             */
  286.     CAS_TIME *Time;            /* CAS time structure.             */
  287.  
  288.     Date = (CAS_DATE *)malloc(sizeof(CAS_DATE));
  289.     if(Date == NULL)
  290.     CASError(CASNOMEM, TRUE, 0);
  291.  
  292.     Time = (CAS_TIME *)malloc(sizeof(CAS_TIME));
  293.     if(Time == NULL)
  294.     CASError(CASNOMEM, TRUE, 0);
  295.  
  296.     /* Find the event, open the file and print the header if it is a new file,
  297.        otherwise just append the info to what is already there. */
  298.     EventHandle = CASFindFirst(ANY_STATUS, SEARCH_FORWARD, LOG_QUEUE);
  299.  
  300.     if(access(FileName,0) == -1) {
  301.     /* File doesn't exist - create it and print header. */
  302.     if((OutFile = fopen (FileName,"w")) == NULL)
  303.         CASError(CASBADLF, TRUE, 0);
  304.     if(Delimiter == SPACE_DELIMIT) {
  305.         fprintf(OutFile, "\nDate             Action   Mode    Pg/Fi  Status   ConnectTime To/From\n");
  306.         fprintf(OutFile,   "-------------------------------------------------------------------------------\n");
  307.     }
  308.     }
  309.  
  310.     else
  311.     /* File exists - open in append mode. */
  312.     if((OutFile = fopen (FileName,"a")) == NULL)
  313.         CASError(CASBADLF, TRUE, 0);
  314.    
  315.     while(EventHandle > 0) {
  316.     /* Fill the TCF buffer with info about the event. */
  317.     GetTaskBuffer(EventHandle, LOG_QUEUE, (char *)TCFbuffer, &FileHandle);
  318.     close(FileHandle);
  319.  
  320.     CorrectDate(TCFbuffer, Date, Time);
  321.  
  322.     switch(TCFbuffer->EventType) {
  323.         case 0: strcpy(action,  "Send"); break;
  324.         case 1: strcpy(action,  "Recv"); break;
  325.         case 2: strcpy(action,  "Send(P)"); break;
  326.         case 4: strcpy(action,  "Recv(P)"); break;
  327.         default: strcpy(action, "Error");
  328.     }
  329.  
  330.     switch(TCFbuffer->TransferType) {
  331.         case 0: strcpy(mode,  "Fine"); break;
  332.         case 1: strcpy(mode,  "Std"); break;
  333.         case 2: strcpy(mode,  "CCP"); break;
  334.         default: strcpy(mode, "     "); break;
  335.     }
  336.  
  337.     switch(TCFbuffer->EventStatus) {
  338.         case 0: strcpy(status, "Complete"); break;
  339.         case 5: strcpy(status, "Canceled"); break;
  340.         default: strcpy(status,"Error"); break;
  341.     }
  342.  
  343.     switch(Delimiter) {
  344.         /* Print info for space delimited output. */
  345.         case SPACE_DELIMIT:
  346.         SpaceIt(OutFile, TCFbuffer, Date, Time, action, mode, status);
  347.         break;
  348.         /* Print info for tab delimited output. */
  349.         case TAB_DELIMIT:
  350.         TabIt(OutFile, TCFbuffer, Date, Time, action, mode, status);
  351.         break;
  352.         /* Print info for comma delimited output. */
  353.         case COMMA_DELIMIT:
  354.         CommaIt(OutFile, TCFbuffer, Date, Time, action, mode, status);
  355.     }
  356.  
  357.     EventHandle = CASFindNext(LOG_QUEUE);
  358.     }
  359.  
  360.     /* Close the log file and delete events if specified. */
  361.     fclose(OutFile);
  362.     if(DelLog)
  363.     if((ErrorCode = CASDeleteAllFiles(LOG_QUEUE)) < 0)
  364.         CASError(CASDELLQ, TRUE, -ErrorCode);
  365. }
  366.  
  367. /*---------------------------------------------------------------------------*
  368.  *                    Main                     *
  369.  *---------------------------------------------------------------------------*
  370.  *  The main function calls functions to parse the command line and execute  *
  371.  *  the routines requested with the command line switches.                   *
  372.  *---------------------------------------------------------------------------*
  373.  *  Return: The DOS exit code is 0 if no errors are encountered, 1 for         *
  374.  *        commandline syntax errors, 2 for CCAM error, and 3 for DOS         *
  375.  *        errors.                                 *
  376.  *---------------------------------------------------------------------------*/
  377. main(int argc, char **argv)
  378. {
  379.  
  380.     int result;             /* Return value from CAS call.         */
  381.     int FileType;            /* Type of output wanted.             */
  382.  
  383.     argc = ParseCommand(argc, argv, SwitchTable, TABLESIZE);
  384.  
  385.     /* If help is wanted, print info and exit (with 0 exit code). */
  386.     if(Help || (argc == 1))
  387.     CASHelp(DescTable, DESCSIZE, HelpTable, HELPSIZE);
  388.  
  389.     /* Insure that the Resident Scheduler is installed. */
  390.     if((result = CASGetInstalledState()) != INSTALLED) {
  391.     if( result == NOTiOK )
  392.         CASError(CASNOHWO, TRUE, 0);
  393.     else
  394.         CASError(CASNOHWN, TRUE, 0);
  395.     }
  396.  
  397.     /* Redirect stdout to a file. */
  398.     if(RedirOut) {
  399.     if((ccout = fopen("OUTPUT.CC", "w")) == NULL)
  400.         CASError(CASOPENTEMP, TRUE, 0);
  401.     if(-1 == dup2(fileno(ccout), 1))
  402.         CASError(CASBADREDIR, TRUE, 0);
  403.     }
  404.  
  405.     /* Check to make sure that a (one only) path name was specified. */
  406.     if(argc < 2)
  407.     CASError(CASARGPATH, TRUE, 0);
  408.     else if(argc > 2)
  409.     CASError(CASARGMULTPATH, TRUE, 0);
  410.  
  411.     /* Get buffer for Task information. */
  412.     TCFbuffer = (ECF *)malloc(sizeof(ECF));
  413.     if(TCFbuffer == NULL)
  414.     CASError(CASNOMEM, TRUE, 0);
  415.  
  416.     /* Get buffer for File Transfer information. */
  417.     FTRbuffer = (FTR *)malloc(sizeof(FTR));
  418.     if(FTRbuffer == NULL)
  419.     CASError(CASNOMEM, TRUE, 0);
  420.  
  421.     /* Make sure that only one switch can be set! */
  422.     if(!Tab && !Comma)
  423.     FileType = SPACE_DELIMIT;
  424.     else if(Tab)
  425.     FileType = TAB_DELIMIT;
  426.     else
  427.     FileType = COMMA_DELIMIT;
  428.  
  429.     /* Point to the filename and print it. */
  430.     argv++;
  431.     ClearLog(*argv, TCFbuffer, FTRbuffer, FileType);
  432.  
  433.     exit(0);
  434. }
  435.